package com.gexin.controller.weixin.subscribe;

 import com.gexin.controller.commom.WeixinTransfer.WeixinTransferUtil;
 import com.gexin.controller.commom.WeixinTransfer.model.WeixinTransferResult;
 import com.jfinal.weixin.sdk.utils.HttpUtils;

 import java.util.*;
 import java.util.logging.Handler;

 import static com.gexin.model.system.RoleCheck.CheckType.url;

public class CompanyToPerson {

    private static final String tranfers = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
    //PathKit.getRootClassPath()+"/weixin/cer/"+


    public void openid(){
        String s = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
                "APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
    }

    public void bankBalance(String keystore_file_path){
        Map map = new HashMap();
        map.put("mch_id","1561327181");
        map.put("partner_trade_no",System.currentTimeMillis() / 1000 + "");
        map.put("nonce_str","123123huuihi324");
        map.put("enc_bank_no","6217000180046782847");
        map.put("sign","32423423");
        map.put("enc_true_name","李");
        map.put("bank_code","1003 ");
        map.put("amount","1");
        map.put("desc","积分兑换");
        String s = generalSign(map);
        map.put("sign",s);

        try {

//       +"/weixin/cer/zhongke_apiclient_cert.p12     String url = "D:/zk_shop21.04/src/main/webapp/WEB-INF/classes/weixin/cer/zhongke_apiclient_cert.p12";
            String url = keystore_file_path;
            String xmlResult = HttpUtils.postSSL(tranfers,  ConvertUtils.mapToXml(map), url, "1561327181");
//
//            String s2 = HttpClientUtil.sendPostParam(tranfers, ConvertUtils.mapToXml(map),map);
            System.out.print(xmlResult);

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    public WeixinTransferResult transfer(String appid, String mchid,
                                         String keystore_file_path, String openid,
                                         String orderId,
                                         String money, String desc){
        String xmlResult = new String();
        Map map = new HashMap<>();
        map.put("mch_appid",appid);
        map.put("mchid",mchid);
        map.put("nonce_str", "123123huuihi324");
        map.put("partner_trade_no",orderId);
        map.put("openid",openid);
        map.put("check_name","NO_CHECK");
        map.put("amount",money);
        map.put("desc",desc);
        String s = generalSign(map);
        map.put("sign",s);
        String s1 = "提现失败";
        try {
             s1 = HttpRequestHandler.httpsRequest(tranfers, ConvertUtils.mapToXml(map),mchid,keystore_file_path);
//       +"/weixin/cer/zhongke_apiclient_cert.p12     String url = "D:/zk_shop21.04/src/main/webapp/WEB-INF/classes/weixin/cer/zhongke_apiclient_cert.p12";
//            String url = keystore_file_path;
//            xmlResult = HttpUtils.postSSL(tranfers,  ConvertUtils.mapToXml(map), url, "1561327181");
//      String url, String xmlObj, String model, String path
//            String s2 = HttpClientUtil.sendPostParam(url, ConvertUtils.mapToXml(map),map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return WeixinTransferUtil.toWeixinTransferResult(s1);

//        return xmlResult;

    }



//    public void transfer(String appid, String mchid,
//                     String keystore_file_path, String openid,
//                     String orderId,
//                     String money, String desc){
//        Map map = new HashMap<>();
//        map.put("mch_appid",appid);
//        map.put("mchid",mchid);
//        map.put("nonce_str", "123123huuihi324");
//        map.put("partner_trade_no",orderId);
//        map.put("openid",openid);
//        map.put("check_name","NO_CHECK");
//        map.put("amount",money);
//        map.put("desc",desc);
//         String s = generalSign(map);
//        map.put("sign",s);
//        try {
////            String url = "D:/zk_shop21.04/src/main/webapp/WEB-INF/classes/weixin/cer/zhongke_apiclient_cert.p12";
//              String url = keystore_file_path;
////            String xmlResult = HttpUtils.postSSL(tranfers,  ConvertUtils.mapToXml(map), url, "1561327181");
////
//            String s1 = HttpClientUtil.sendPostParam(tranfers, ConvertUtils.mapToXml(map),map);
//            System.out.print(s1);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//
//        }
//    }

    public static String generalSign(Map map) {
        StringBuffer sb = new StringBuffer();
        Set set = map.keySet();
        List<String> result = new ArrayList<>(set);
        Collections.sort(result);
        for(int i=0;i<result.size();i++){
            String s = result.get(i);
            String v = (String) map.get(s);
            sb.append(s + "=" + v + "&");
        }
        sb.append("key").append("=").append("2e9e2c3fb695ab27637de08734fb991f");
        return  MD5Util.MD5Encode(sb.toString(), "utf-8").toUpperCase();
    }
}
